﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace quizzer_linq_3
{
    public partial class QuestionPopup : Form
    {
        //Local version of the BoardQuestion passed into this class for use in other methods
        public BoardQuestion parameterQuestion;

        //Local version of the Quizzer object for use in updating the score
        public Quizzer localQuizzer;

        //List of buttons to add to the form
        private List<Button> buttonList;

        //List of all possible answers
        List<string> possibleAnswersList;

        //From the database, this the number of answers that cannot be null
        const int NUMBER_OF_NOT_NULL_ANSWERS = 2;

        //From the database, this the number of possible answers to pull from the database
        const int NUMBER_OF_POSSIBLE_ANSWERS = 5;

        /**Constructor: QuestionPopup(BoardQuestion, Quizzer)
         * Creates necessary object for use in the question popup
         * 
         * @param questionFromBoard The  question sent to the popup from the gameboard
         * @param quizzerFromScore
         * */
        public QuestionPopup(BoardQuestion questionFromBoard, Quizzer quizzerForScore)
        {
            InitializeComponent();

            parameterQuestion = questionFromBoard;
            localQuizzer = quizzerForScore;
            questionText.Text = questionFromBoard.content;

            buttonList = new List<Button>();
            possibleAnswersList = new List<string>();

            PopulateLists();

            this.Name = questionFromBoard.topic;

            buttonList = RandomizeButtonList(buttonList);
            for (int answerCount = 0; answerCount < possibleAnswersList.Count; answerCount++)
            {
                buttonList[answerCount].Text = possibleAnswersList[answerCount];
                buttonList[answerCount].Click += new EventHandler(QuestionPopup_Click);
            }

            checkForBlankButtons();
        }

        /**Method: checkForBlankButtons
         * This method checks to make sure all buttons actually have an answer assoicated with them. If not, it removes the button
         * 
         * */
        void checkForBlankButtons()
        {
            Button[] blankButtons = new Button[NUMBER_OF_POSSIBLE_ANSWERS - NUMBER_OF_NOT_NULL_ANSWERS];
            string[] answers = new string[NUMBER_OF_POSSIBLE_ANSWERS - NUMBER_OF_NOT_NULL_ANSWERS];

            blankButtons[0] = answerButton2;
            blankButtons[1] = answerButton3;
            blankButtons[2] = answerButton4;

            answers[0] = parameterQuestion.wrongAnswers[1];
            answers[1] = parameterQuestion.wrongAnswers[2];
            answers[2] = parameterQuestion.wrongAnswers[3];

            for (int blankCounter = 0; blankCounter < answers.Length; blankCounter++)
            {
                if (answers[blankCounter].Trim() == "")
                {
                    blankButtons[blankCounter].Visible = false;
                }
            }
        }

        /**Method: PopulateLists
         * This adds the buttons and answers to the correct lists associated with them
         * 
         * */
        void PopulateLists()
        {
            addToLists(answerButton0, parameterQuestion.correctAnswer);
            addToLists(answerButton1, parameterQuestion.wrongAnswers[0]);
            addToLists(answerButton2, parameterQuestion.wrongAnswers[1]);
            addToLists(answerButton3, parameterQuestion.wrongAnswers[2]);
            addToLists(answerButton4, parameterQuestion.wrongAnswers[3]);
        }

        /**Method: LoadGameSet
         * This method handles each element in the buttonList and possibleAnswerList
         * 
         * @param button the button to add to buttonList
         * @param answer the answer to add to possibleAnswers
         * */
        void addToLists(Button button, string answer)
        {
            if (answer.Trim() != "")
            {
                buttonList.Add(button);
                possibleAnswersList.Add(answer);
            }
        }
        
        /**Method: RandomizButtonList
         * @returns List<Button> with the buttons in a random order
         * This method will randomize the answers in the QuestionPopup
         * 
         * @param buttonsRandomized List<Button> that starts off in order and is randomized and returned at the end
         * */
        private List<Button> RandomizeButtonList(List<Button> buttonsRandomized)
        {
            Button placeholder = new Button();
            Random randomNumber = new Random();
            for (int numberOfSwaps = 20; numberOfSwaps > 0; numberOfSwaps--)
            {
                int indexToSwap = randomNumber.Next(0, buttonsRandomized.Count);
                placeholder = buttonsRandomized[indexToSwap];
                buttonsRandomized.Remove(placeholder);
                buttonsRandomized.Add(placeholder);
            }
            return (buttonsRandomized);
        }

        private Button[] RandomizeArray(Button[] original)
        {
            Button savedButton = new Button();
            Button[] scrambledAnswers = new Button[original.Length];
            scrambledAnswers = original;
            Random randomNumber = new Random();
            savedButton = original[0];

            for (int numberOfSwaps = 20; numberOfSwaps > 0; numberOfSwaps--)
            {
                int indexToSwap = randomNumber.Next(0, original.Length);
                scrambledAnswers[0] = scrambledAnswers[indexToSwap];
                scrambledAnswers[indexToSwap] = savedButton;
                savedButton = scrambledAnswers[0];
            }
            return scrambledAnswers;
        }

        /** Method: QuestionPopup_Click
         * @author Spencer Estes
         * The event handler for clicking an answer
         * 
         * @param sender sender of the action
         * @param e event is triggered
        */
        void QuestionPopup_Click(object sender, EventArgs e)
        {
            CheckAnswer((Button)sender);
        }

        /**Method: CheckAnswer
         * This method checks whether the button you clicked is the correct answer and also increments your score by 100 if correct or decrements by 100 if incorrect.
         * 
         * @param answerToCheck the button that holds the Answer we want to compare to the correct answer
         * */
        public void CheckAnswer(Button answerToCheck)
        {
            if (parameterQuestion != null)
            {
                if (answerToCheck.Text == parameterQuestion.correctAnswer)
                {
                    System.Windows.Forms.MessageBox.Show("Got the correct answer!");
                    localQuizzer.publicScore += 100;
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("Incorrect Answer...\n\nThe correct answer was " + parameterQuestion.correctAnswer + ".");
                    localQuizzer.publicScore -= 100;
                    localQuizzer.addToTopicsIncorrect(parameterQuestion.topic);
                }
                localQuizzer.UpdateScore();
                localQuizzer.publicButton.Visible = false;
                localQuizzer.publicButton.Enabled = false;
                this.Close();
            }
            localQuizzer.questionAnswered++;
            localQuizzer.checkNumberQuestionsAnswered();
        }
    }
}
